﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Security;
using System.Reflection;

namespace PasswordProvider.View.Library
{
    /// <summary>
    /// UserControl for editing a password in the login site details dialog. Enables secure editing of a password
    /// with option of viewing the password in clear text. For additional security, the existing password is not 
    /// decrypted unless and until requested.
    /// </summary>
    public sealed partial class EncryptedValueEditBox : UserControl
    {
        #region Private fields, Constructor

        private bool _updatingTextBoxText;

        // Constructor
        public EncryptedValueEditBox()
        {                      
            InitializeComponent();
            _textBox.TextChanged +=
                delegate
                {
                    if (_updatingTextBoxText)
                        return;

                    DisplayString = _textBox.Text;
                };
        }

        #endregion

        #region Dependency Properties

        /// <summary>
        /// Dependency property IsReadOnlyProperty.
        /// </summary>
        public static readonly DependencyProperty IsReadOnlyProperty =
           DependencyProperty.Register("IsReadOnly", typeof(bool), typeof(EncryptedValueEditBox),
           new PropertyMetadata(
               (d, e) =>
               {
                   (d as EncryptedValueEditBox)._textBox.IsReadOnly = (bool)e.NewValue;
               }));
        /// <summary>
        /// Get or set whether the edit box should be read-only. This is a dependency property.
        /// </summary>
        public string IsReadOnly
        {
            get { return (string)GetValue(IsReadOnlyProperty); }
            set { SetValue(IsReadOnlyProperty, value); }
        }


        /// <summary>
        /// Dependency Property, get or set the text that is actually displayed.
        /// </summary>
        public static readonly DependencyProperty DisplayStringProperty =
           DependencyProperty.Register("DisplayString", typeof(string), typeof(EncryptedValueEditBox),
           new PropertyMetadata(
               (d, e) =>
               {
                   (d as EncryptedValueEditBox)._textBox.Text = e.NewValue as string;
               }));
        /// <summary>
        /// Get or set the text that is displayed to the user. This is a dependency property.
        /// </summary>
        public string DisplayString
        {
            get { return (string)GetValue(DisplayStringProperty); }
            set { SetValue(DisplayStringProperty, value); }
        }

        #endregion // Dependency Properties

        #region Private

       
        private void SetTextBoxText(string text)
        {
            _updatingTextBoxText = true;
            _textBox.Text = text;
            _updatingTextBoxText = false;
        }

        #endregion

    }
}
